/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../PacketCreator.h"
#include "../../server/Player.h"
#include "../../net/world/Guild.h"
#include "../../net/world/GuildMembers.h"
#include "../../net/world/GuildMember.h"

PacketWriter* PacketCreator::createGuild() {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x01);
	return &pw;
}

PacketWriter* PacketCreator::changeGuildEmblem() {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x11);
	return &pw;
}

PacketWriter* PacketCreator::guildInfo(Guild* guild) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x1a);
	if (guild == 0) {
		pw.write(0);
		return &pw;
	}
	pw.write(1);
	pw.writeInt(guild->getID());
	pw.writeString(guild->getGuildName());
	for (int i = 0; i < 5; i++) {
		pw.writeString(guild->getRank(i));
	}
	GuildMembers* gmembers = guild->getMembers();
	hash_map <int, GuildMember*> members = *gmembers->getMembers();
	pw.write((char)members.size());
	for(hash_map<int, GuildMember*>::iterator iter = members.begin(); iter != members.end(); iter++) {
		pw.writeInt(iter->second->getID());
	}
	for(hash_map<int, GuildMember*>::iterator iter = members.begin(); iter != members.end(); iter++) {
		pw.writeString(iter->second->getName(), 13);
		pw.writeInt(iter->second->getJob());
		pw.writeInt(iter->second->getLevel());
		pw.writeInt(iter->second->getRank());
		pw.writeInt((gmembers->isMemberOnline(iter->second->getID())) ? 1 : 0);
		pw.writeBytes("91940491"); // Signature
		pw.writeInt(3); // Alliance Rank
	}
	// Guild info
	pw.writeInt(guild->getCapacity());
	pw.writeShort(guild->getLogoBG());
	pw.write(guild->getLogoBGColor());
	pw.writeShort(guild->getLogo());
	pw.write(guild->getLogoColor());
	pw.writeString(guild->getNotice());
	pw.writeInt(guild->getGuildPoints());
	pw.writeInt(0); // Alliance ID
	return &pw;
}

PacketWriter* PacketCreator::addGuildPlayer(int gid, GuildMember* member) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x27);
	pw.writeInt(gid);
	pw.writeInt(member->getID());
	pw.writeString(member->getName(), 13);
	pw.writeInt(member->getJob());
	pw.writeInt(member->getLevel());
	pw.writeInt(member->getRank());
	pw.writeInt(1);
	pw.writeInt(1);
	pw.writeInt(3);
	return &pw;
}

PacketWriter* PacketCreator::guildPlayerLeave(int gid, int charid, std::string charname, bool expelled) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(expelled ? 0x2f : 0x2c);  //0x2c for leaving, 0x2f for expelled
	pw.writeInt(gid);
	pw.writeInt(charid);
	pw.writeString(charname);
	return &pw;
}

PacketWriter* PacketCreator::inviteGuild(string name, int gid) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x05);
	pw.writeInt(gid);
	pw.writeString(name);
	return &pw;
}

PacketWriter* PacketCreator::changeRank(int guildid, int userid, int rank) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x40);
	pw.writeInt(guildid);
	pw.writeInt(userid);
	pw.write(rank);
	return &pw;
}

PacketWriter* PacketCreator::changeGuildCapacity(int gid, int capacity) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x3a);
	pw.writeInt(gid);
	pw.write(capacity);
	return &pw;
}

PacketWriter* PacketCreator::updateGuildRanks(int gid, string rank1, string rank2, string rank3, string rank4, string rank5) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x3e);
	pw.writeInt(gid);
	pw.writeString(rank1);
	pw.writeString(rank2);
	pw.writeString(rank3);
	pw.writeString(rank4);
	pw.writeString(rank5);
	return &pw;
}

PacketWriter* PacketCreator::updateGuildNotice(int gid, string notice) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x44);
	pw.writeInt(gid);
	pw.writeString(notice);
	return &pw;
}

PacketWriter* PacketCreator::disbandGuild(int gid) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x32);
	pw.writeInt(gid);
	pw.write(1);
	return &pw;
}

PacketWriter* PacketCreator::addGuildPoint(Guild *guild) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x48);
	pw.writeInt(guild->getID());
	pw.writeInt(guild->getGuildPoints());
	return &pw;
}

PacketWriter* PacketCreator::showGuildEmblem(Guild* guild) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x42);
	pw.writeInt(guild->getID());
	pw.writeShort(guild->getLogoBG());
	pw.write(guild->getLogoBGColor());
	pw.writeShort(guild->getLogo());
	pw.write(guild->getLogoColor());
	return &pw;
}

PacketWriter* PacketCreator::guildMemberOnline(int gid, int cid, bool online) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x3d);
	pw.writeInt(gid);
	pw.writeInt(cid);
	pw.write(online ? 1 : 0);
	return &pw;
}

PacketWriter* PacketCreator::denieGuildInvite(string name) {
	pw.writeShort(GUILD_OPERATION);
	pw.write(0x37);
	pw.writeString(name);
	return &pw;
}
